<template>
	<view class="mine-page page">
		<CustomNav title="个人中心" :isBack="true"></CustomNav>
		<view class="info-block">
			<image class="img" :src="user.avatar"></image>
			<view class="name" v-if="user.phone">{{user.name}}</view>
			<button hover-class="none" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" v-else
				class="user-name">点击登录</button>
		</view>
		<view class="menu-block">
			<view class="menu-item" @click="navigate('/page_pack/user/user')">
				<view class="left-item">
					<image src="@/static/image/mine.png" class="menu-img"></image>
					<view class="name">个人信息</view>
				</view>
				<view class="iconfont icon-gengduo"></view>
			</view>
			<view class="menu-item" @click="navigate('/page_pack/user/my_record')">
				<view class="left-item">
					<image src="@/static/image/disaster-reporting.png" class="menu-img"></image>
					<view class="name">我的报灾记录</view>
				</view>
				<view class="iconfont icon-gengduo"></view>
			</view>
			<view class="menu-item" @click="navigate('/page_pack/user/my_animal')">
				<view class="left-item">
					<image src="@/static/image/animal-record.png" class="menu-img"></image>
					<view class="name">我的野生动物记录</view>
				</view>
				<view class="iconfont icon-gengduo"></view>
			</view>
			<view class="menu-item" @click="navigate('/page_pack/record/record')" v-if="state==1">
				<view class="left-item">
					<image src="@/static/image/whole.png" class="menu-img"></image>
					<view class="name">报灾记录</view>
				</view>
				<view class="iconfont icon-gengduo"></view>
			</view>
			<view class="menu-item" @click="logout" v-if="user.phone">
				<view class="left-item">
					<image src="@/static/image/exit.png" class="menu-img"></image>
					<view class="name">退出登录</view>
				</view>
				<view class="iconfont icon-gengduo"></view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getStorage,
		removeStorage,
		showToast
	} from '@/util/auth';
	import {
		savePhoneNumberByCode,
		getWhiteList
	} from '@/api/user.js'
	import {
		getWxLoginCode
	} from '@/util/user.js'
	export default {
		data() {
			return {
				user: {
					avatar: '',
					name: ''
				},
				openIdList: [],
				openId: '',
				state: 0
			}
		},
		methods: {
			async getPhoneNumber(e) {
				if (!e.detail.code) {
					this.showToast('您取消了授权');
					return;
				}
				if (!getStorage("token")) {
					let code = await getWxLoginCode();
					uni.showLoading({
						title: '加载中',
						mask: true,
					})
					let res = await this.$store.dispatch('user/getWxLoginCode', code).catch(() => {
						uni.showModal({
							title: '提示',
							content: '获取信息失败，请重试',
						})
					})
				}
				uni.showLoading({
					title: '获取中',
					mask: true
				})
				savePhoneNumberByCode({
					code: e.detail.code
				}).then((res) => {
					this.user = res
					this.$store.dispatch('user/getUser')
				})
			},
			logout() {
				uni.showModal({
					title: '提示',
					content: `确定要退出重新登录吗？`,
					confirmColor: '#027BFD',
					success: (res) => {
						if (res.confirm) {
							removeStorage('token');
							removeStorage('user');
							this.user = ''
							uni.navigateTo({
								url: '/pages/index/index'
							})
						}
					}
				})
			},
			getWhiteList() {
				getWhiteList().then(res => {
					this.openIdList = res || [];
					this.openIdList.forEach((item, index) => {
						if (item.openId == this.openId) {
							this.state = 1
						}
					})
				})
			},
		},
		onShow() {
			let user = this.$store.getters.user.user;
			if (user) {
				Object.keys(this.user).forEach(item => {
					this.user[item] = user[item]
				})
			}
			if (getStorage('user')) {
				this.getWhiteList()
				this.user = getStorage('user')
				this.openId = this.user.openId
			}
		}
	}
</script>

<style lang="scss" scoped>
	.mine-page {
		overflow: hidden;

		.info-block {
			background: linear-gradient(to right, #3466FF, #9804FD);
			padding: 100rpx 30rpx;
			display: flex;
			align-items: center;
			margin-top: 20%;

			.img {
				width: 160rpx;
				height: 160rpx;
				border-radius: 50%;
				background-color: #F7F6F9;
				margin-right: 30rpx;
			}

			.name {
				color: #fff;
				font-size: 36rpx;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
				display: flex;
				width: calc(100% - 160rpx);
			}

			.user-name {
				font-size: 36rpx;
				font-weight: bold;
				background: none;
				margin: 0;
				border-radius: 0;
				line-height: normal;
				padding: 0;
				color: #fff;

				&::after {
					border: none;
				}
			}
		}

		.menu-block {
			margin-top: -50rpx;
			background: #F7F6F9;
			padding: 50rpx 30rpx;
			border-radius: 50rpx 50rpx 0 0;

			.menu-item {
				padding: 50rpx 30rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				background-color: #fff;
				border-radius: 20rpx;
				margin-bottom: 20rpx;

				.left-item {
					display: flex;
					align-items: center;

					.menu-img {
						width: 40rpx;
						height: 40rpx;
						margin-right: 20rpx;
					}
				}

				.iconfont {
					font-weight: bold;
					color: #767676;
				}
			}
		}
	}
</style>